package com.example.wm_mbp.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * ClassName: MyMetaObjecthandler
 * Package: com.example.wm_mbp.config
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-03-20
 * Time: 23:03
 */

/**
 * 自定义原数据处理器
 * 公共字段自动填充
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入的时候自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        //设置要填充的字段
        metaObject.setValue("createTime", new Date());
        metaObject.setValue("updateTime",new Date());
        //创建人的id id是从HttpServletRequest获取 但是这里没有id
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }

    /**
     * 修改的时候 自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {

        metaObject.setValue("updateTime",new Date());


        //创建人的id id是从HttpServletRequest获取 但是这里没有id
        metaObject.setValue("updateUser",BaseContext.getCurrentId());

    }
}
